using Cysharp.Threading.Tasks;
using ShawnFramework;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    // 输入
    private float _horizontalInput;
    private float _verticalInput;
    
    [Header("形象节点")]
    public SpriteRenderer playerSprite;
    [Header("瞄准节点")]
    public SpriteRenderer aimSprite;
    [Header("相机聚焦点")] 
    public Transform focusPoint;
    
    [Header("子弹发射间隔")] 
    public float bulletDuration = 0.5f;
    [Header("移动速度")]
    public float moveSpeed = 5f;
    [Header("检索范围")] 
    public float checkRadius = 5f;

    [Header("检测触碰敌人距离")] 
    public float hurtRadius;
    [Header("存在瞄准目标时，相机可移动半径")]
    public float focusRadius;
    
    // 临时变量
    private Collider2D[] _nearbyCollider;
    // 当前瞄准目标
    private Transform _aimTarget;

    private void Start()
    {
        aimSprite.Active(false);
        _nearbyCollider = new Collider2D[100];
    }

    private void Update()
    {
        ReceiveInputInfo();
        Move();
        Aim();
        Shoot();
    }

    /// <summary>
    /// 接收输入信息
    /// </summary>
    private void ReceiveInputInfo()
    {
        _horizontalInput = Input.GetAxisRaw("Horizontal");
        _verticalInput = Input.GetAxisRaw("Vertical");
    }

    float _bulletTimer = 0f;
    /// <summary>
    /// 移动
    /// </summary>
    private void Move()
    {
        var currPos = transform.position;
        var moveDir = new Vector3(_horizontalInput, _verticalInput, 0).normalized;
        var targetPos = currPos + moveDir * (moveSpeed * Time.deltaTime);
        transform.position = Vector3.MoveTowards(currPos, targetPos, moveSpeed * Time.deltaTime);
    }

    /// <summary>
    /// 射击
    /// </summary>
    private void Shoot()
    {
        if (_bulletTimer < bulletDuration)
        {
            _bulletTimer += Time.deltaTime;
            return;
        }
        _bulletTimer = 0f;

        var ctl = GameController.Instance;
        
        // 瞄准阶段
        var curPos = transform.position;
        var cnt = Physics2D.OverlapCircleNonAlloc(
            curPos
            , checkRadius
            , _nearbyCollider 
            , 1 << LayerMask.NameToLayer("Enemy")
        );
        if (cnt > 0)
        {
            var nearby = _nearbyCollider[0].transform;
            _aimTarget = nearby;
            
            // 锁定方向
            var dir = (nearby.position - transform.position).normalized;
            var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
            // 发射子弹
            var bulletObj = ObjectPool.Global.Spawn(PoolElemTypeEnum.Bullet00, ctl.bulletPrefab);
            var bullet = bulletObj.GetComponent<Bullet>();
            bullet.ApplyFlyParams(dir);
            bullet.Position(transform.position);
            bullet.LocalEuler(new Vector3(0, 0, angle));
            bullet.Active(true);
            ObjectPool.Global.Recycle(PoolElemTypeEnum.Bullet00, bulletObj, 3f);
        }
        else
        {
            _aimTarget = null;
        }
    }

    /// <summary>
    /// 瞄准
    /// </summary>
    private void Aim()
    {
        // 没有瞄准目标时，相机聚焦本体
        if (!_aimTarget)
        {
            aimSprite.Active(false);
            focusPoint.Position(transform.position);
            return;
        }
        aimSprite.Active(true);
        var aimPos = _aimTarget.position;
        aimSprite.Position(aimPos);
        var selfToOtherDir = (aimPos - transform.position).normalized;
        focusPoint.LocalPosition(selfToOtherDir * focusRadius);
    }
    
    private void OnDrawGizmos()
    {
        Gizmos.color = Color.yellow;
        Gizmos.DrawWireSphere(transform.position, checkRadius);
        
        Gizmos.color = Color.green;
        Gizmos.DrawWireSphere(transform.position, focusRadius);
    }
}
